// pages/my/dispatchstate/dispatchstate.js
Page({
  data: {
    id: "",
    detail: {},
    list: [{
      reviewStatus: 1
    }],
    toggle: true
  },
  onLoad: function (options) {
    this.setData({
      id: options.id
    });
  },
  onShow: function () {
    this.getData();
  },
  getData() {
    getApp().globalData.http.detail({
      id: this.data.id
    }).then(res => {
      this.setData({
        detail: res.data
      });
    });
    getApp().globalData.http.takenList({
      id: this.data.id
    }).then(res => {
      this.setData({
        list: res.data
      });
    });
  },
  pass(e) {
    if (this.data.toggle) {
      this.setData({
        toggle: false
      });
      getApp().globalData.vant.toast.loading({
        message: "审核中...",
        duration: 0,
        forbidClick: true
      });
      getApp().globalData.http.evidence({
        id: this.data.id,
        historyId: e.currentTarget.dataset.historyid,
        reviewStatus: 2
      }).then(res => {
        getApp().globalData.vant.toast({
          type: "success",
          message: "审核成功",
          duration: 1000,
          onClose: () => {
            this.getData();
            this.setData({
              toggle: true
            });
          },
        });
      }).catch(e => {
        getApp().globalData.vant.toast({
          type: "fail",
          message: e.msg,
          duration: 1000,
          onClose: () => {
            this.setData({
              toggle: true
            });
          },
        });
      });
    }
  },
  reject(e) {
    wx.navigateTo({
      url: "/pages/my/auditreject/auditreject?taskId=" + this.data.id + "&historyId=" + e.currentTarget.dataset.historyid + "&reviewStatus=" + e.currentTarget.dataset.reviewstatus,
    });
  },
  toDestail(e) {
    wx.navigateTo({
      url: `/pages/my/dispatchstatedestail/dispatchstatedestail?id=${this.data.detail.id}&takenUserId=${e.currentTarget.dataset.userid}&historyId=${e.currentTarget.dataset.historyid}&reviewStatus=${e.currentTarget.dataset.reviewstatus}`,
    });
  },
  batch() {
    let ids = [];
    let list = this.data.list;
    list.forEach(item => {
      if (item.reviewStatus == 1 || item.reviewStatus == 4) {
        ids.push(item.historyId);
      }
    });
    if (ids.length > 0) {
      getApp().globalData.vant.dialog.confirm({
          title: "批量通过?",
          message: "确定批量通过用户完成的任务吗?",
          confirmButtonText: "确定",
          confirmButtonColor: "#33B868"
        })
        .then(() => {
          getApp().globalData.http.evidence({
            id: this.data.id,
            historyIds: ids.join(","),
            reviewStatus: 2
          }).then(res => {
            getApp().globalData.vant.toast({
              type: "success",
              message: "审核成功",
              duration: 1000,
              onClose: () => {
                this.getData();
              },
            });
          });
        })
        .catch(() => {});
    } else {
      getApp().globalData.vant.toast("没有可审核的用户");
    }
  },
  previewImg(e) {
    let arr = e.currentTarget.dataset.img;
    let current = e.currentTarget.dataset.current;
    getApp().globalData.util.previewImage(arr, current);
  }
})